package perso.marcr.util;

public class ConverterByte {	

	public static double convert(double value, Unit cFrom, Unit cTo)
	{
		double result = 0;
		if(cFrom.getValue() > cTo.getValue())
		{
			result = value * ( Math.pow(1024., (cFrom.getValue()-cTo.getValue())));
		}
		else if(cTo.getValue() > cFrom.getValue())
		{
			result = value / ( Math.pow(1024., (cTo.getValue()-cFrom.getValue())));
		}
		else
		{
			result = value;
		}

		return result;
	}

	/**
	 * @author Marc Rabahi
	 * @param value in Byte
	 * @param cUnit of the value
	 * @return a string containing the value in human readable way.
	 */
	public static String toHumanReadable(double value, Unit cUnit)
	{
		double dValueYo=convert(value, cUnit, Unit.YO);
		if(dValueYo >= 1)
		{
			return dValueYo+" Yo";
		}

		double dValueZo=convert(value, cUnit, Unit.ZO);
		if(dValueZo >= 1)
		{
			return dValueZo+" Zo";
		}

		double dValueEo=convert(value, cUnit, Unit.EO);
		if(dValueEo >= 1)
		{
			return dValueEo+" Eo";
		}

		double dValuePo=convert(value, cUnit, Unit.PO);
		if(dValuePo >= 1)
		{
			return dValuePo+" Po";
		}

		double dValueTo=convert(value, cUnit, Unit.TO);
		if(dValueTo >= 1)
		{
			return dValueTo+" To";
		}

		double dValueGo=convert(value, cUnit, Unit.GO);
		if(dValueGo >= 1)
		{
			return dValueGo+" Go";
		}

		double dValueMo=convert(value, cUnit, Unit.MO);
		if(dValueMo >= 1)
		{
			return dValueMo+" Mo";
		}

		double dValueKo=convert(value, cUnit, Unit.KO);
		if(dValueKo >= 1)
		{
			return dValueKo+" Ko";
		}

		double dValueOctet=convert(value, cUnit, Unit.OCTET);
		return dValueOctet+" Octet";
	}

	/**
	 * @author Marc Rabahi
	 * @param value in Byte
	 * @param cUnit of the value
	 * @return a string containing the value in human readable way.
	 */
	public static String toHumanReadableSpeed(double value, Unit cUnit)
	{
		return toHumanReadable(value, cUnit)+"/s";
	}
}
